#include "settingdialogview.h"
#include "ui_settingdialogview.h"
#include "settingconfig.h"
#include "config.h"
#include <QFileDialog>
#include <QDebug>
#include <QDir>

SettingDialogView::SettingDialogView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingDialogView)
{
    ui->setupUi(this);

    Config *config = Config::defaultConfig();
    SettingConfig *setting = config->settingConfig();
    ui->lineEdit->setText(setting->downLoadPath());
    ui->lineEdit->setEnabled(false);

    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(selectDownLoadPath()));
}

SettingDialogView::~SettingDialogView()
{
    delete ui;
}

void SettingDialogView::cancel()
{
    accept();
}

void SettingDialogView::confirm()
{
//    Config *config = Config::defaultConfig();
//    SettingConfig *setting = config->settingConfig();
//    setting->setDownLoadPath(ui->lineEdit->text());
//    setting->saveSetting(config->documentPath()+"/setting.data");
//    setting->saveToFilePath();
    accept();
}

void SettingDialogView::accept()
{
    QString dirPath = ui->lineEdit->text();

    if(QFile::exists(dirPath)){
        Config *config = Config::defaultConfig();
        SettingConfig *setting = config->settingConfig();
        setting->setDownLoadPath(ui->lineEdit->text());
//        setting->saveSetting(config->documentPath()+"/setting.data");
        setting->saveToFilePath();
    }
    QDialog::accept();
}

void SettingDialogView::selectDownLoadPath()
{
    QFileDialog fileDialod(this);
    fileDialod.setFileMode(QFileDialog::Directory);
    int accept =fileDialod.exec();
    if(accept==1){
        QString dir = fileDialod.selectedFiles().at(0);
        ui->lineEdit->setText(dir);
    }

}
